package zabbix

import (
	"encoding/json"
	"errors"
)


func ApplicationCreateData() (string,map[string]interface{}) {
	method := "application.create"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "name": "SNMP Items",
        "hostid": "10050"
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func ApplicationDeleteData() (string,map[string]interface{}) {
	method := "application.delete"
	var data map[string]interface{}
	jsonstr := []byte(`[
        "356",
        "358"
	]`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func ApplicationGetData() (string,map[string]interface{}) {
	method := "application.get"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "output": "extend",
        "hostids": "10001",
        "sortfield": "name"
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func ApplicationMassaddData() (string,map[string]interface{}) {
	method := "application.massadd"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "applications": [
            {
                "applicationid": "247"
            },
            {
                "applicationid": "246"
            }
        ],
        "items": [
            {
                "itemid": "22800"
            },
            {
                "itemid": "22801"
            }
        ]
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func ApplicationUpdateData() (string,map[string]interface{}) {
	method := "application.update"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "applicationid": "13",
        "name": "Processes and performance"
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}


func ApplicationCreate(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := ApplicationCreateData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func ApplicationDelete(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := ApplicationDeleteData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func ApplicationGet(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := ApplicationGetData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func ApplicationMassadd(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := ApplicationMassaddData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func ApplicationUpdate(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := ApplicationUpdateData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}


